<?php
include('step.php');

class teststep extends step {
    private $in = true;
    private $out = true;
    public function transform(data $data) {
        $data->fieldnames=array(0=>"Reichtum");
        $data->rows=array(0=>array(0=>3500));
        $data->success=true;
        echo "teststep\n";
        return $data;
    }
}
class dummystep extends step {
    private $in = true;
    private $out = true;
    public function transform(data $data) {
        $data->success=true;
        echo "dummystep\n";
        return $data;
    }
}
class readcsv extends step {
    private $in = false;
    private $out = true;
    public $headerrow = true;
    public $filename;
    public $fieldseparator;
    public function __construct($filename, $fieldseparator, $headerrow) {
        $this->filename = $filename;
        $this->fieldseparator = $fieldseparator;
        $this->headerrow = $headerrow;
    }
    public function transform(data $data){
        try {
            $file = fopen($this->filename,'r');
            if($file) {
                if($this->headerrow) {
                    $row = fgets($file);
                    if($row){
                        $data->fieldnames = explode($this->fieldseparator, $row);
                    }
                }
                while($row = fgets($file)) {
                    $data->rows[] = explode($this->fieldseparator, $row);
                }
                fclose($file);
                $data->success=true;
                echo "readcsvstep\n";
                return $data;
            } else {
                throw new Exception("Fehler beim Öffnen von Datei $this->filename");
            }
        } catch(Exception $e) {
            echo $e->getMessage();
        }
    }
}
class var_dumpstep extends step {
    private $in = true;
    private $out = true;
    public function transform(data $data) {
        var_dump($data);
        $data->success=true;
        echo "var_dumpstep\n";
        return $data;
    }
}
class echofieldnamesstep extends step {
    private $in = true;
    private $out = true;
    public function transform(data $data) {

        foreach($data->fieldnames as $fieldname) {
            $fieldnamesline .= $fieldname."\t";
        }
        trim($fieldnamesline);
        echo "$fieldnamesline"."\n";
        $data->success = true;
        echo "echofieldnamesstep\n";
        return $data;
    }
}
class tableoutputstep extends step {
    private $in = true;
    private $out = false;
    public $system;
    public $host;
    public $user;
    public $password;
    public $db;
    public $table;
    public $mapping = array();
    public $ds;
    public function __construct($system, $host, $user, $password, $db, $table, $mapping = array()) {
        $this->system = $system;
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->db = $db;
        $this->table = $table;
        $this->mapping = $mapping;
        try{
            $this->ds = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
           
            $this->mapping = $mapping;
        } catch(Exception $e) {
            echo $e->getMessage();
            
        }
    }
    public function transform(data $data) {
        try{
            if($this->ds) {
                
                if(count($this->mapping) == 0) {
                    
                    reset($data->rows);
                    
                    foreach($data->rows as $row) {
                        
                        $sql = "insert into $this->table (".implode(',', $data->fieldnames).") ";
                        $sql .= "values(".implode(',', $row).")";
                        $this->ds->exec($sql);
                    }
                    
                    $data->fieldnames = array();
                    $data->rows = array();
                    $data->success = true;
                }
            } else {
                throw new Exception("no database");
            }
        } catch(Exception $e){
            $data->success = false;
            $data->fieldnames = array();
            $data->rows = array();
            echo $e->getMessage();
        }
        echo "tableoutputstep\n";
        return $data;
    }
}
class randomtablestep extends step {
    private $numberfields;
    private $numberrows;
    private $from;
    private $to;
    public function __construct($numberfields, $numberrows, $from, $to) {
        $this->numberfields = $numberfields;
        $this->numberrows = $numberrows;
        $this->from = $from;
        $this->to = $to;
    }
    public function transform(data $data) {
        try {
            for($i = 0; $i < $this->numberrows; $i++){
                for($j = 0; $j < $this->numberfields; $j++){
                    $row[$j] = rand($this->from, $this->to);
                }
                $data->rows[$i] = $row;
            }
            $data->fieldnames = array();
            $data->success = true;
            echo "randomarraystep\n";
            return $data;
        } catch(Exception $e) {
            $data->success = false;
            $data->fieldnames = array();
            $data->rows = array();
            echo $e->getMessage();
        }
    }
}
class memorystep extends step{
    private $memory;
    private $name;
    private $return;

    public function __construct($name, $memory, $return = true) {
        $this->memory = $memory;
        $this->name = $name;
        $this->return = $return;
    }
    public function transform(data $data) {
        try {
           if($this->memory[$this->name] != null) {
               throw new Exception("memory entry $this->name already exists");
           }
           $this->memory[$this->name] = $data;
            $data->success = true;
            echo "memorystep\n";
            if($this->return){
                return $data;
            } else {
                return new data;
            }
        } catch(Exception $e) {
            $data->success = false;
            $data->fieldnames = array();
            $data->rows = array();
            echo $e->getMessage();
        }
    }
}
?>
